<?php

namespace App\Models\Operation;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class ContentTemplateDetails extends Model
{
    use SoftDeletes;

    public $table = 'content_template_details';

    protected $dates = ['deleted_at'];

    public $fillable = [
        // 所属内容模板ID
        'content_template_id',
        // 名称
        'name',
        'title',
        'subtitle',
        'start_at',
        'end_at',
        'online_at',
        'offline_at',
        'publish_at',
        // 模板别名
        'short_desc',
        'online_status',
        'publish_status',
        'publish_down_status',
        'moderrate_status',
        'user_id',
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        // 所属模板ID
        'content_template_id',
        'name'                => 'string',
        'title'               => 'string',
        'subtitle'            => 'string',
        'short_desc'          => 'string',
        'online_status'       => 'integer',
        'publish_status'      => 'integer',
        'publish_down_status' => 'integer',
        'moderate_status'     => 'integer',
        'user_id'             => 'integer',
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * 内容模板
     **/
    public function contentTemplate()
    {
        return $this->hasOne(\App\Models\Operation\ContentTemplate::class, 'id', 'content_template_id');
    }
    /**
     * 过滤上下线数据
     **/
    public function scopeOnlineStatus($query, $status = 1)
    {
        return $query->where('online_status', $status);
    }

    /**
     * 过滤上线发布数据
     **/
    public function scopeOnlinePublished($query, $status = 1)
    {
        return $query->where(['online_status' => 1, 'publish_status' => $status]);
    }

    /**
     * 过滤下线发布数据
     **/
    public function scopeOfflinePublished($query, $status = 1)
    {
        return $query->where(['online_status' => 0, 'publish_down_status' => $status]);
    }
}
